﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CP.Controllers.GPM;
using CP.Entities;
using CP.Utility;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.Utils.Menu;
using DevExpress.XtraGrid.Localization;
using DevExpress.XtraGrid.Views.Grid;

namespace GPM
{
    public partial class FrmDepartment : BaseForm
    {
        public GridCheckMarksSelection selection;
        public FrmDepartment()
        {
            InitializeComponent();
            this.BarComponents = components;
        }
        //
        public void RefreshDept()
        {
            DepartmentController dc = new DepartmentController();
            this.gcDepartment.DataSource = dc.GetAllDepartments();
        }

        private void barQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshDept();
        }

        private void barAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            FrmEditDepartment frmedit = new FrmEditDepartment();
            frmedit.ShowDialog(this);
        }

        private void barEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (selection.SelectedCount != 1)
            {
                ExMessageBox.Show("提示信息：请选中一条记录编辑", AppSetter.SoftName);
                return;
            }
            Department info = selection.Selection[0] as Department;
            if (null == info)
            {
                ExMessageBox.Show("提示信息：选中的记录不存在", AppSetter.SoftName);
                return;
            }
            FrmEditDepartment frmedit = new FrmEditDepartment("1", info.dept_id);
            frmedit.ShowDialog(this);
        }

        private void barDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (0 == selection.SelectedCount)
            {
                ExMessageBox.Show("提示信息：请选中要删除的记录！", AppSetter.SoftName);
                return;
            }

            if (ExMessageBox.Confirm("提示信息：确定要删除选中的记录吗？", AppSetter.SoftName) == DialogResult.No)
                return;

            IList<int> deptids = new List<int>();
            foreach (object o in selection.Selection)
            {
                Department dept = o as Department;
                if (null != dept)
                    deptids.Add(dept.dept_id);
            }
            try
            {
                DepartmentController dc = new DepartmentController();
                dc.DeleteDepts(deptids);
                ExMessageBox.Show("提示信息：删除成功！", AppSetter.SoftName);
                RefreshDept();
            }
            catch (Exception ex)
            {
                ExMessageBox.Show(ex.Message, AppSetter.SoftName);
            }
        }

        private void FrmDepartment_Load(object sender, EventArgs e)
        {
            selection = new GridCheckMarksSelection(this.gvDepartment);
            RefreshDept();
        }

        private DXMenuItem GetItemByStringId(DXPopupMenu menu, GridStringId id)
        {
            foreach (DXMenuItem item in menu.Items)
                if (item.Caption == GridLocalizer.Active.GetLocalizedString(id))
                    return item;
            return null;
        }

        private void gvDepartment_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            if (e.MenuType == GridMenuType.Column)
            {
                // Customize
                DXMenuItem miCustomize = GetItemByStringId(e.Menu, GridStringId.MenuColumnColumnCustomization);
                if (miCustomize != null)
                    miCustomize.Enabled = false;

                //// Group By This Column
                //DXMenuItem miGroup = GetItemByStringId(e.Menu, GridStringId.MenuColumnGroup);
                //if (miGroup != null)
                //    miGroup.Enabled = false;

                // Group By This Column
                DXMenuItem miRemoveColumn = GetItemByStringId(e.Menu, GridStringId.MenuColumnRemoveColumn);
                if (miRemoveColumn != null)
                    miRemoveColumn.Enabled = false;
            }
        }
    }
}
